package 复习.博客.回溯算法.组合问题;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class 组合总和 {
    List<List<Integer>> lists = new ArrayList<>();
    List<Integer> list = new ArrayList<>();

    int Target;
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        Target = target;
//        Arrays.sort(candidates);为啥加了之后更高了呢
        backStacking(candidates,0,0);
        return lists;
    }

    public void backStacking(int[] candidates,int sum,int index){
        if(sum == Target){
            lists.add(new ArrayList<>(list));
        }
        for(int i = index;i < candidates.length && sum + candidates[i] <= Target;i++){
            list.add(candidates[i]);
            backStacking(candidates,sum+candidates[i],i);
            list.remove(list.size() - 1);
        }
    }
}
